package com.laikan.library.managers;

import android.app.Activity;
import android.content.Intent;
import com.socks.library.KLog;

import java.util.ArrayList;

/**
 * 管理Activity
 *
 * @author anrun
 */
public class ActivityMgr {

    private static ActivityMgr mInstance;
    private ArrayList<Activity> mActivityList;

    private ActivityMgr() {

        mActivityList = new ArrayList<>();
    }

    public static ActivityMgr getInstance() {

        if(mInstance == null) {
            mInstance = new ActivityMgr();
        }
        return mInstance;
    }

    public void pushActivity(Activity activity) {

        // 里面是否有次activity 保证只有一个
        String activityFullName = activity.getClass().getName();
        Activity ac = findActivity(activityFullName);
        if(ac != null) {
            ac.finish();
        }
        mActivityList.add(activity);
    }

    public void removeActivity(Activity activity) {

        if(mActivityList.contains(activity)) {
            mActivityList.remove(activity);
        }
    }

    public Activity getTopActivity() {

        if(mActivityList.size() > 0) {
            Activity screen = mActivityList.get(mActivityList.size() - 1);
            return screen;
        }else {
            return null;
        }
    }

    public int getActivityCount() {

        return mActivityList.size();
    }

    public void popActivity() {

        if(mActivityList.size() > 0) {
            Activity screen = mActivityList.remove(mActivityList.size() - 1);
            screen.finish();
        }
    }

    public void clearActivity() {

        Activity screen = null;
        while(mActivityList.size() > 0) {
            screen = mActivityList.remove(mActivityList.size() - 1);
            if(screen != null) {
                screen.finish();
            }
        }
    }

    /**
     * @param index index of activity in list
     *
     * @return Activity instance of activity
     *
     * @function get Activity by Index
     */
    public Activity getActivityByIndex(int index) {

        return mActivityList.get(index);
    }

    /**
     * @function get actvity by classname(full package and classname)
     */
    public Activity findActivity(String classname) {

        if(classname == null) {
            return null;
        }
        for(int index = 0; index < mActivityList.size(); index++) {
            Activity ac = mActivityList.get(index);
            if(ac != null) {
                if(ac.getComponentName().getClassName().equals(classname)) {
                    return ac;
                }
            }
        }
        return null;
    }

    /**
     * 打开activity
     *
     * @param activityClazz
     */
    public void startActivity(Class activityClazz) {

        Activity topActivity = getTopActivity();
        if(topActivity != null) {
            KLog.d("TopActivity=" + topActivity.getClass().getName());
            topActivity.startActivity(new Intent(topActivity, activityClazz));
        }
    }

    /**
     * 关闭activity
     *
     * @param activityClazz
     */
    public void finishActivity(Class activityClazz) {

        Activity ac = findActivity(activityClazz.getName());
        if(ac != null) {
            ac.finish();
        }
    }
}
